#!/usr/bin/env python
# -*- coding: utf-8 -*- 

#Kiwi Bot
#Copyright (C) 2009 Mathieu "MathX" Xhonneux
#Copyright (C) 2010 @nge@utclancon.com

#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License along
#with this program; if not, write to the Free Software Foundation, Inc.,
#51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import kiwi, kiwi.config, kiwi.clients
from os.path import isfile
import glob

class Lang:
	def __init__(self, main, module):
		self.main = main
		self.lang_by_defaut = kiwi.getPath(kiwi.config.configuration().lang.default)
		self.base = kiwi.getPath('@kiwi/languages/')
		self.module = module+'.lang'
	def lang(self, event, data):
		if event:
			if event == int:
				lang = self.main.clients.getClientById(int(event))[0].live
			else:
				lang = self.main.clients.getClientById(event.id)[0].live
		else :
			lang = self.lang_by_defaut
		path = '%s%s/%s' %(self.base,lang,self.module)
		if not isfile(path):
			path = '%s%s/%s' %(self.base,self.lang_by_defaut,self.module)
		if len(data) == 6: #Correction d'un bug. Affichage de l'aide d'une commande .
			file = glob.glob('%s%s/*.*' %(self.base,lang))
			for dir in file:
				f = open(dir,'r')
				d = f.readlines()
				f.close()
				for list in d :
					help = list.split('=')
					if data == help[0].strip() :
						return help[1].strip()
					# if e[0:6] == data :
						# return e[9:-1] # fin.
			return "Erreur pls contact Creator."
		f=open(path, 'r')
		t = f.readlines()
		f.close()
		for list in t:
			help = list.split('=')
			if data == help[0].strip() :
				return help[1].strip()
		return "Error in plugin 'lang', pls contact Creator."
		